<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option dis v-for="dict in daiban" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="接收时间" prop="repairTime">
        <el-date-picker clearable v-model="receiveTime" @change="handleSearchTimeChange" type="daterange"
          range-separator="-" start-placeholder="开始时间" end-placeholder="结束时间" value-format="YYYY-MM-DD">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="daibanList" @selection-change="handleSelectionChange">
      <el-table-column type="index" width="55" align="center" />
      <el-table-column label="唯一标识" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="内容" align="center" prop="content" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <el-switch :disabled="scope.row.status==='已办'" :model-value="scope.row.status" inline-prompt class="mb-2" active-text="已办" active-value="已办" 
          inactive-text="待办" inactive-value="待办" @change="handleChangeStatus($event, scope.row)"/>
        </template>
      </el-table-column>
      <el-table-column label="接收时间" align="center" prop="startDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="endDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="耗时" align="center" prop="allDate" />
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script setup name="Daiban">
import { listDaiban, updateDaiban } from "@/api/system/daiban";
import { ElMessage } from "element-plus";

const { proxy } = getCurrentInstance();
const { daiban } = proxy.useDict('daiban');

const daibanList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const receiveTime = ref('')
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    status: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询待办信息列表 */
function getList() {
  loading.value = true;
  listDaiban(queryParams.value).then(response => {
    daibanList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  receiveTime.value = ''
  queryParams.value.startDate = null
  queryParams.value.endDate = null
  proxy.resetForm("queryRef");
  handleQuery();
}
const handleSearchTimeChange = (item) => {
  queryParams.value.startDate = item[0]
  queryParams.value.endDate = item[1]
}

const handleChangeStatus = async(value, item) => {
  item.status = value
  const res = await updateDaiban(item)
  ElMessage.success(res.msg)
  getList()
}
getList();
</script>
